/************************************************************************
* Programmer:  Philippe Gambling
*
* Course:  CSCI 4534
*
* Date:  February 20, 2009
*
* Environment:  GCC 3.2.2 running on diamond.rocks.uhcl.edu
*
* Files Included: node.h, node.c
*
* Purpose:  Implements a simple generic node structure for a linked list.
*
***********************************************************************/ 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "node.h"


Node new_Node(size_t datasize, const void *src) 
{
/************************************************************************
* Purpose:  To create a new Node
*
* Parameters:  (size_t) size of memory to allocate for data, 
*              (void *) source data to copy to node's data.
*
* Action:  Allocates and returns a new Node.
*
***********************************************************************/
  Node n = (Node)malloc(sizeof(struct node));
  if(n == NULL) { return NULL; }

  n->_pdata = (void *)malloc(sizeof(datasize));
  memcpy(n->_pdata,src,datasize);

  n->NextPtr = NULL;
  
  return n;
}

void free_Node(Node n) 
{
/************************************************************************
* Purpose:  To free up memory associated with Node.
*
* Parameters:  (Node) target Node.
*
* Action:  Free memory pointed to by _pdata, and frees the node memory
*
***********************************************************************/
  free(n->_pdata); n->_pdata = NULL;
  free(n); n = NULL;
}

void Node_CopyData(const Node n, void * dest, size_t size)
{
/************************************************************************
* Purpose:  To copy memory from node's data to another location.
*
* Parameters:  (Node) target Node, (void *) memcpy destination, amount to copy
*
* Action:  Use memcpy to copy size bytes of data from _pdata to dest.
*
***********************************************************************/
  memcpy(dest, n->_pdata, size);
}
